<template>
    <div>
        <el-dialog title="编辑会员" v-model="props.isShow" :before-close="closeX" @open="open(formRef)">
            <el-form :model="form" label-width="5rem" :rules="rules" ref="formRef">
                <el-form-item label="手机号" prop="phone">
                    <el-input v-model="form.phone"></el-input>
                </el-form-item>
                <el-form-item label="昵称" prop="nickname">
                    <el-input v-model="form.nickname"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form.password" placeholder="留空则不修改！"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="close()">取消</el-button>
                    <el-button type="primary" @click="submit(formRef)">确定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup>
import store from '@/store';
import type { UploadProps, FormRules, FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus'
import { ref, reactive, defineProps, defineEmits, defineExpose } from "vue";
import { memberinfo, memberedit } from '@/request/api';
let emit = defineEmits(['close'])
let formRef = ref<FormInstance>()
let props = defineProps({
    isShow: Boolean

})


let form = ref({
    phone: '',
    nickname: '',
    password: '',
    status: 2,
    uid: 0
})
// 关闭
let close = () => {
    emit("close")
}

const closeX = (done: any) => {
    done()
    close()
}

let open = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    form.value = {
        uid:0,//会员编号，必填项
        nickname:'',//昵称
        phone:'',//手机号
        password:'',//密码
        status:2//状态1正常2禁用

    }
    formEl.resetFields();
}
let submit = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((val) => {
        if (val) {
             console.log(form.value);
             
             memberedit(form.value).then((res: any) => {
                if (res.code == 200) {
                    get()
                    close()

                    ElMessage({
                        message: res.msg,
                        type: 'success',
                    })
                } else {
                    ElMessage({
                        message: res.msg,
                        type: 'warning',
                    })
                }
            })

        }
    })
}
let lookup = (uid: number) => {
    memberinfo({ uid }).then((res: any) => {
        console.log(res)
        form.value = res.list
        form.value.uid = uid
        form.value.password = ''
    })


}
let rules = reactive<FormRules>({
    phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
    nickname: [{ required: true, message: "请输入市场价格", trigger: "blur" }],
    password: [{ required: true, message: "请输入密码", trigger: "blur" }]
})
let get = () => {
    store.dispatch('member/getMemberListActions')
}
get()
defineExpose({
    lookup
})

</script>